﻿using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace JinYuan.ControlLib
{
    public partial class CircleProgramBar : Control
    {
        public CircleProgramBar()
        {

            //设置控件样式，去除缓冲
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            InitControl();
            this.SizeChanged += delegate
            {
                this.Invalidate(); //重绘控件
            };


        }

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        #endregion

        /// <summary>
        /// 初始化控件参数
        /// </summary>
        private void InitControl()
        {
            this.Width = 200;
            this.Height = 200;
        }

        private Color bgColor = Color.FromArgb(224, 224, 224);
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示背景边框颜色")]
        public Color BgColor
        {
            get { return this.bgColor; }
            set
            {
                this.bgColor = value;
                this.Invalidate();
            }
        }

        private Color sectorColor = Color.FromArgb(109, 179, 63);
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示扇形颜色")]
        public Color SectorColor
        {
            get { return this.sectorColor; }
            set
            {
                this.sectorColor = value;
                this.Invalidate();
            }
        }

        private int borderWidth = 5;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示边框宽度")]
        public int BorderWidth
        {
            get { return this.borderWidth; }
            set
            {
                this.borderWidth = value;
                this.Invalidate();
            }
        }

        private int maxValue = 100;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示进度最大值")]
        public int MaxValue
        {
            get { return this.maxValue; }
            set
            {
                this.maxValue = value;
                this.Invalidate();
            }
        }


        private bool isShowDataType = false;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置显示数值是小数还是整数")]
        public bool IsShowDataType
        {
            get { return this.isShowDataType; }
            set
            {
                this.isShowDataType = value;
                this.Invalidate();
            }
        }


        private int intValue = 0;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示整数进度值")]
        public int IntValue
        {
            get { return this.intValue; }
            set
            {
                if (value > this.maxValue)
                {
                    return;
                }
                this.intValue = value;
                this.Invalidate();
            }
        }

        private int floatValue = 0;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示小数进度值")]
        public int FloatValue
        {
            get { return this.floatValue; }
            set
            {
                if (value > this.maxValue)
                {
                    return;
                }
                this.floatValue = value;
                this.Invalidate();
            }
        }


        private bool setShowsign = false;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示百分号，显示百分号需要设置MaxValue值为100")]
        public bool SetShowsign
        {
            get { return this.setShowsign; }
            set
            {
                if (this.maxValue > 100)
                {
                    this.maxValue = 100;
                }
                this.setShowsign = value;
                this.Invalidate();
            }
        }

        //对Control进行绘制
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (this.Width < borderWidth * 4 || this.Height < borderWidth * 4)
            {
                return;
            }
            var g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;//消除锯齿，也就是抗锯齿什么鬼东西
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;


            RectangleF bg_rectangle = new RectangleF(0 + borderWidth, 0 + borderWidth, Width - borderWidth * 2, Height - borderWidth * 2);    //控件整体坐标

            g.DrawEllipse(new Pen(bgColor, borderWidth), bg_rectangle);  //画背景圆



            Rectangle rl = new Rectangle(0 + borderWidth * 2, 0 + borderWidth * 2, this.Width - borderWidth * 4, this.Height - borderWidth * 4);

            decimal topAngle = (isShowDataType ? this.floatValue : this.intValue * 1.0M / this.maxValue) * 360M;//计算进度条划过的度数
            g.FillPie(new SolidBrush(sectorColor), rl, 0, (float)topAngle);   //填充扇形

            if (this.setShowsign)
            {
                SizeF proValSize = g.MeasureString(isShowDataType ? this.floatValue.ToString() : this.intValue.ToString() + "%", this.Font);//计算文字的范围 

                g.DrawString(isShowDataType ? this.floatValue.ToString() : this.intValue.ToString() + "%", this.Font, new SolidBrush(this.ForeColor),

                    bg_rectangle.X + bg_rectangle.Width / 2 - proValSize.Width / 2, bg_rectangle.Y + bg_rectangle.Height / 2 - proValSize.Height / 2);
            }
            else
            {
                SizeF proValSize = g.MeasureString(isShowDataType ? this.floatValue.ToString() : this.intValue.ToString(), this.Font);//计算文字的范围 

                g.DrawString(isShowDataType ? this.floatValue.ToString() : this.intValue.ToString(), this.Font, new SolidBrush(this.ForeColor),
                    bg_rectangle.X + bg_rectangle.Width / 2 - proValSize.Width / 2, bg_rectangle.Y + bg_rectangle.Height / 2 - proValSize.Height / 2);
            }
        }
    }
}
